package practice02;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: practice02
 * @ClassName: UserB
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/12 19:47
 * @Copyright 大牛版板所有
 */
public class UserB implements IUserBiz {
    User[] users = new User[2];

    public UserB() {
        users[0] =new User("admin","张三","123456","admin@123.com");
        users[1] =new User("tom","李四","123456","tomcat@cat.com");
    }


    @Override
    public void register(String username, String password, String rePassword, String name,
                         String email) throws IllegalRegisterException{
        boolean flag = false;
        for (User us:this.users) {
            if (username.equals(us.getName())){
                flag = true;
                break;
            }
            else {
                if (!(password.equals(rePassword))){
                    throw new IllegalRegisterException("出现异常，两次输入的密码不一样！");
                }
            }        }
        if (flag){
            throw new IllegalRegisterException("出现异常，用户名已经存在！");
        }

        System.out.println("注册成功！");
    }

    @Override
    public void login(String username, String password) throws IllegalLoginException{
        boolean flag= false;
        for (User us:this.users) {
            if (username.equals(us.getName())){
                flag = true;
                if (!(password.equals(us.getPassword()))){
                    throw new IllegalLoginException("出现异常，用户名与密码不匹配！");
                }
                break;
            }

        }
        if (!flag){
            throw new IllegalLoginException("出现异常，用户名不存在！");
        }
        System.out.println("登录成功！");
    }
}
